package name.vaccari.matteo.tai.phonedirectory.web;

import name.vaccari.matteo.tai.phonedirectory.model.PeopleRepository;
import name.vaccari.matteo.tai.phonedirectory.model.Person;
import name.vaccari.matteo.tai.phonedirectory.network.Servlet;

public class PeopleServlet implements Servlet {

	private final PeopleRepository repository;

	public PeopleServlet(PeopleRepository repository) {
		this.repository = repository;
	}

	public String doGet(String string) {
		String heading = "<h1>People Directory</h1>";
		String tableStart = "<table>";
		String rows = "";
		for (Person person : repository.getPeople(0, 10)) {
			rows += convertPersonToTableRow(person);
		}
		String tableEnd = "</table>";
		return heading  + tableStart  + rows + tableEnd ;
	}

	public String convertPersonToTableRow(Person person) {
		return "<tr><td>" + person.getLastName() +
				"</td><td>" + person.getFirstName() +
				"</td><td>" + person.getMobile() +
				"</td></tr>";
	}

}
